package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.OperationEmpRecordAddDTO;
import org.jsola.hr.dto.OperationEmpRecordUpdateDTO;
import org.jsola.hr.query.OperationEmpRecordQuery;
import org.jsola.hr.service.IOperationEmpRecordService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 操作员工记录表
 *
 * @author zhr
 */
@RestController("hrOperationEmpRecordController")
@RequestMapping(HrConstants.API_PATH + "/v1/operationEmpRecords")
public class OperationEmpRecordController implements IOperationEmpRecordControllerAPI {

    @Autowired
    private IOperationEmpRecordService operationEmpRecordService;

    @Override
    @PostMapping("/postQueryEmpPageList")
    public Result<Page<EmpInfoVO>> postQueryEmpPageList(@Valid @RequestBody OperationEmpRecordQuery operationEmpRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                operationEmpRecordService.queryEmpPageList(operationEmpRecordQuery, tokenUser)
        );
    }

    @Override
    @GetMapping("/queryEmpPageList")
    public Result<Page<EmpInfoVO>> queryEmpPageList(@Valid OperationEmpRecordQuery operationEmpRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                operationEmpRecordService.queryEmpPageList(operationEmpRecordQuery, tokenUser)
        );
    }

    @Override
    @GetMapping("/queryCheckInPageList")
    public Result<Page<CheckInPeopleListVO>> queryCheckInPageList(@Valid OperationEmpRecordQuery operationEmpRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                operationEmpRecordService.queryPageList(operationEmpRecordQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/postQueryCount")
    public Result<OperationEmpRecordListVO> postQueryCount(@Valid @RequestBody OperationEmpRecordQuery operationEmpRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                operationEmpRecordService.queryCount(operationEmpRecordQuery, tokenUser)
        );
    }

    @Override
    @GetMapping("/queryCount")
    public Result<OperationEmpRecordListVO> queryCount(@Valid OperationEmpRecordQuery operationEmpRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        return Result.success(
                operationEmpRecordService.queryCount(operationEmpRecordQuery, tokenUser)
        );
    }

    @Override
    @PostMapping
    public Result<OperationEmpRecordVO> save(@Valid @RequestBody OperationEmpRecordAddDTO operationEmpRecordAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增操作员工记录表权限校验

        return Result.success(
                operationEmpRecordService.save(operationEmpRecordAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody OperationEmpRecordUpdateDTO operationEmpRecordUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改操作员工记录表权限校验

        int count = operationEmpRecordService.update(operationEmpRecordUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{operationEmpRecordId}")
    public Result<Integer> delete(@PathVariable String operationEmpRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除操作员工记录表权限校验

        int count = operationEmpRecordService.deleteByIds(tokenUser, operationEmpRecordId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] operationEmpRecordIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除操作员工记录表权限校验

        int count = operationEmpRecordService.deleteByIds(tokenUser, operationEmpRecordIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{operationEmpRecordId}")
    public Result<OperationEmpRecordVO> findById(@PathVariable String operationEmpRecordId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看操作员工记录表权限校验

        return Result.success(
            operationEmpRecordService.selectById(operationEmpRecordId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<OperationEmpRecordListVO>> listAll(@Valid OperationEmpRecordQuery operationEmpRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看操作员工记录表权限校验

        return Result.success(
            operationEmpRecordService.select(operationEmpRecordQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<OperationEmpRecordListVO>> page(@Valid OperationEmpRecordQuery operationEmpRecordQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            operationEmpRecordService.selectPage(operationEmpRecordQuery, tokenUser.getSiteId())
        );
    }


}

